﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMS.Model;
using PMS.DBUtility;
using System.Data.OleDb;

namespace PMS.DAL
{
    public class CategoryDAL
    {
        private readonly string strsql = "SELECT Id,Name,Pid,Path FROM P_Category";
        private readonly string strsql2 = "INSERT INTO P_Category (Pid,[Name],Path) values(?,?,?)";
        public bool ExistByName(string name)
        {
            string sql = "select Id from P_Category where [Name]='" + name + "'";
            if (SQLHelper.Execute(sql)>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public  bool Insert(Category category)
        {
            if (!ExistByName(category.Name))
            {
                OleDbParameter[] parameters = new OleDbParameter[3];
                parameters[0] = new OleDbParameter("?",category.Pid);
                parameters[1] = new OleDbParameter("?",category.Name);
                parameters[2] = new OleDbParameter("?",category.Path);
                if (SQLHelper.Execute(strsql2, parameters) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                

            }
            else
            {
                return false;
            }
        }
        public  bool Delete(string Id)
        {
            //TODO
            return true;
        }
        public  bool Update(string id, string Name)
        {
            return true;
        }
        public  IList<Category> GetList()
        {
            List<Category> categorylist = new List<Category>();       
            System.Data.DataSet ds = SQLHelper.GetData(strsql);
            System.Data.DataTable table = ds.Tables[0];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                Category c = new Category();
                System.Data.DataRow row = table.Rows[i];
                c.Id = (int)row[0];
                c.Name = (string)row[1];
                c.Pid = (string)row[2];
                c.Path = (string)row[3];
                categorylist.Add(c);
                c = null;
            }
            return categorylist;
        }
    }
}
